<?php

declare(strict_types=1);

namespace app\yperp\common;


class Redis extends  \think\cache\driver\Redis
{
    private static $instance;
    public static function getInstance($options = [])
    {
        if (self::$instance === null) {
            self::$instance = new self($options);
        }
        return self::$instance;
    }
    /**
     * 推送数据到redis
     * @param $str
     * @param $Options
     * @return bool
     */
    public function publish($channel, $message)
    {
        $res =  $this->handler->publish($channel, json_encode($message));
        return $res ? true : false;
    }


    public function subscribe($channel = [], $func)
    {
        $redis = $this->handler;
        $this->handler->subscribe($channel, function ($instance, $channel, $message) use ($func, $redis) {
            $error_type_ = $redis->get('error_type_' . md5($message));
            $error_type_ = $error_type_ ?: 0;
            if ($error_type_ < 3) {
                $class = $func['className']::getInstance();

                call_user_func_array([$class, $func['method']], [$channel, $message]);
            }
        });
    }

    /**
     * 推送数据到redis
     * @param $str
     * @param $Options
     * @return string|bool
     */
    function rpushQueue($key, $Options)
    {

        if (is_string($Options)) {
            $this->handler->rpush($key, json_encode($Options));
        }
        if (is_array($Options)) {
            foreach ($Options as $k => $Option) {
                $this->handler->rpush($key, json_encode($Option));
            }
        }

        return true;
    }

    /**
     * 从队列中拉取数据
     * @param $key
     * @return array|bool
     */
    function lpopQueue($key)
    {

        //list类型出队操作
        $value =  $this->handler->lpop($key);
        if ($value) {
            return json_decode($value, true);
        } else {
            return false;
        }
    }

    /**
     * 删除队列
     * @param $queueKey
     * @return bool
     */
    function deleteQueue($queueKey)
    {
        // 获取队列长度
        $queueLength =  $this->handler->llen($queueKey);
        // 逐个弹出元素直到队列为空
        for ($i = 0; $i < $queueLength; $i++) {
            $this->handler->lpop($queueKey);
        }
        return true;
    }


    /**
     * 推送数据到redis
     * @param $name
     * @param $value
     * @return bool
     */
    function rpush($name = 'Options', $value = '')
    {
        if (is_string($name) && $value !== '') {
            $name = [$name => $value];
        }
        if (is_array($name)) {
            foreach ($name as $k => $item) {
                $this->handler->rpush($k, json_encode($item));
            }
        }


        return true;
    }


    /**
     * 从队列中拉取数据
     * @param $Options
     * @return string|bool
     */
    function lpop($key = 'Options')
    {

        //list类型出队操作
        $value =  $this->handler->lpop($key);
        if ($value) {
            return json_decode($value, true);
        } else {
            return false;
        }
    }


    /**
     * 从队列中拉取数据
     * @param string $queueKey
     * @return string|bool
     */
    function ldelete($queueKey = 'Options')
    {

        // 获取队列长度
        $queueLength =  $this->handler->llen($queueKey);

        // 逐个弹出元素直到队列为空
        for ($i = 0; $i < $queueLength; $i++) {
            $this->handler->lpop($queueKey);
        }

        return true;
    }
}
